<?php
namespace app\admin\controller;

use think\facade\App;
use think\Session;
use think\Request;
use think\Db;
use think\facade\Cache;

class Device extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index(Request $request)
    {
        // 设备管理首页逻辑
        $res= Db::name('device_info')->fetchSql(false)->where('device_mac','like','%    ')->select();
        
        foreach ($res as $k=>$v){
            $device_mac=str_replace(array("\r\n", "\r", "\n","\t"), "", $res[$k]['device_mac']);
            Db::name('device_info')->where('id', $res[$k]['id'])->fetchSql(false)->update(['device_mac' => $device_mac]);
        }

        return $res;
    }

    // 添加设备信息
    public function submitDeviceInfo(Request $request)
    {
        $data= $request->param();
        foreach ($data as $k=>$v){
            $data[$k]=str_replace(array("\r\n", "\r", "\n","\t"), "", $data[$k]);
        }
        
        if(!empty($data)) {
            $userId = Db::name('device_info')->fetchSql(false)->insertGetId($data);
            if($userId > 1){
                return $this->buildSuccess($userId,'提交成功',ReturnCode::SUCCESS);
            }else{
                return $this->buildFailed(ReturnCode::LOGIN_ERROR,'', $userId);
            }
        }else{
            return $this->buildFailed(ReturnCode::LOGIN_ERROR,'','没有数据信息');
        }
    }

    // 设备列表信息
    public function deviceListInfo(Request $request)
    {
        $res= Db::table('device_info')
            ->where('device_state','=',1)->fetchSql(false)
            ->select();

        if(!empty($res)) {
            foreach ($res as $k => $v) {
                // 设备信息处理逻辑
                $res_data[$k]['key'] = $v['id'];
                $res_data[$k]['label'] = $this->getdevicetype($v['device_type']).$v['device_mac'];
                $res_data[$k]['mac'] = $v['device_mac'];
                // ... 其他字段处理
            }
            return $this->buildSuccess($res_data, '', ReturnCode::SUCCESS);
        }else{
            return $this->buildFailed(ReturnCode::LOGIN_ERROR,'','没有数据信息');
        }
    }

    // 获取设备类型名称
    public function getdevicetype($type){
        $arr=array(
            '0'=>'注册机',
            '1'=>'考勤机',
            '2'=>'4G平板考勤机',
            '3'=>'测温平板',
            '4'=>'平板',
            '5'=>'海思平板',
            '6'=>'老版平板',
            '7'=>'数据卡',
            '8'=>'路由器',
            '9'=>'其他',
        );
        return $arr[$type];
    }

    // 其他设备相关方法...
}